<?php

/*
 * Category Class
 * ===============================================
 * Date: March 17th, 2012
 * Created By: Integration Team
 * ===============================================
 */

/**
 * contains the functionality to work with Product Categories 
 */


class categoryClass {
    
    protected $categorydisplayattributes = array(); //holds a category's display template
    protected $categoryHierarchy = array();         //holds the category hierarchy tree - Only categoryIds 
    protected $categoryNames = array();             //Holds the categoryNames and is used to normalize the names
                                                    //will be used by the getCategoryName function 
    //test
    
    public function __construct() 
    {
        //get the category displayattributes
        //This will be used by the product object to sort 
        //attributes and headers - using this method the amount of query time 
        //is reduced for each product - Only the templates for categories having products 
        //is required
        
        $query = "select cda.categoryid as categoryId, 
                  cda.templatetype as templateType, 
                  hn.headerid as headerId, 
                  hn.name as headerName, 
                  an.attributeid as attributeId, 
                  an.name as attributeName
                  from ".Spex_DB_name.".categorydisplayattributes cda 
                  join ".Spex_DB_name.".categoryheader ch on cda.headerid = ch.headerid 
                  and cda.templatetype = ch.templatetype 
                  and cda.categoryid = ch.categoryid 
                  join ".Spex_DB_name.".headernames hn on cda.headerid = hn.headerid 
                  and hn.localeid = ".Locale." 
                  join ".Spex_DB_name.".attributenames an on cda.attributeid = an.attributeid 
                  and an.localeid = ".Locale."
                  where cda.categoryid in (select distinct categoryid from ".Spex_DB_name.".product )";
        
           $query.=" order by 
                   cda.templatetype, 
                   ch.defaultdisplayorder,
                   cda.defaultdisplayorder
                   ";
        $result = query($query);
        
        while($row = mysql_fetch_assoc($result))
            
        {
            $this->categorydisplayattributes[$row['categoryId']][$row['templateType']][$row['headerId']][$row['headerName']][$row['attributeId']][$row['attributeName']] =array() ;
        }
        
        mysql_free_result($result);
        
        $this->populate_category_hierarchy();
        
        
    }
    public function getCategoryDisplayAttributes($categoryId=0)
    {
        
        if($categoryId==0)
        {
            return $this->categorydisplayattributes;        
            
        }
        else
        {
            return $this->categorydisplayattributes[$categoryId];
        }
    }
    
    private function populate_category_hierarchy() 
    {
    /**
     * Populates the category hierarchy from the database
     * This would be used to generate breadcrumbs for a product 
     * or provide the delimited string of categories for categorizing a 
     * product into multiple categories (Multiple shopping carts support this)
     * 
     * Sets the attribute $categoryHierarchy as Array(categoryid=>array(categoryid=Name))
     * with the first category in the associated Array being the category itself
     */
        //Mapped type should be supported and needs to be provisioned in 
        //@to-do
        
        
        $categoriesQuery = "Select c.categoryid as categoryid,
                            cn.name as name 
                            from ".Spex_DB_name.".category 
                            c join ".Spex_DB_name.".categorynames cn on 
                            c.categoryid = cn.categoryid and cn.localeid = ".Locale."
                            where c.categoryid > 1";
        $categoriesResult = query($categoriesQuery);
        while($categoryRow = mysql_fetch_assoc($categoriesResult))
        {
            //assign the categoryid and category name to the category Names array
            $this->categoryNames[$categoryRow['categoryid']] = $categoryRow['name'];
            
            $categoryId = $categoryRow['categoryid'];
                    
                    do {
                        $query = "Select c.categoryid as categoryid,
                        c.parentcategoryid as parentcategoryid,
                        c.catlevel as catlevel
                        from category c 
                        where c.categoryid = " . $categoryId;

                        $result = query($query);
                        $row = mysql_fetch_array($result);
                        $this->categoryHierarchy[$categoryRow['categoryid']][]=$row['categoryid'];
                        //The category ids pointing to the category in question 
                        //need to maintain the order in which they are added so that 
                        //hierarchies can be returned later - the categoryhierarchy will hold
                        //the categoryid=>array(array(categoryid,parentcategory,parentsparentid...))
                        
                        $parentcategoryid = $row['parentcategoryid'];
                        $categoryId = $row['parentcategoryid'];
                    } while ($parentcategoryid > 1);

                
        }
    
        
    }
    
    public function get_category_tree($categoryid = 1, $names=TRUE,$delimter='>')
    {
     /**
     * Returns the hierarchy of the passed category 
     * Sets the attribute $categoryHierarchy as Array(categoryid=>array(categoryid=Name))
     * with the first category in the associated Array being the category itself
     * @param $categoryid - the categoryId for which the category shoud be returned 
     * @param $names - TRUE/FALSE specify TRUE if the tree returned should be the names
     *                of the categories "Mobile computers>Notebooks..." 
     *                will default to category Ids otherwise 
     * @param $delimiter - specify the delimiter of the string 
     * Returns String  
     * 
     */
        
        $returnvalue = "";
        
        if(!is_numeric($categoryid)){
            return false;
        }
        //get the requested category's hierarchy from the stored values 
        if(isset($this->categoryHierarchy[$categoryid])){
            //reverse the hierarchy in order to get the topmost category to leaf 
            $categoryHierarchy = array_reverse($this->categoryHierarchy[$categoryid]);
            
            foreach ($categoryHierarchy as $category)
            {
                if($names==TRUE)
                {
                    $returnvalue.=$this->get_category_name($category).$delimter;
                }
                else
                {
                    $returnvalue.=$category.$delimter;
                }
            }
        }
        
        return trim($returnvalue,$delimter);
    }
    
    public function get_category_name($categoryId=0)
    {
        //Returns the associated category name from the normalized array
        //@param $categoryId - The categoryId for which the name needs to be returned 
        //Returns String 
        
        if(isset($this->categoryNames[$categoryId]))
        {
            return $this->categoryNames[$categoryId];
            
        }
        else
        {
            return "";
        }
        
        
    }
}

?>
